package com.hitsoysauce.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class ParameterServlet extends HttpServlet {

	private static final long serialVersionUID = -2922143739743886129L;
	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//request.getRequestDispatcher("/showBeanResult.jsp").forward(request, response);
		
		response.setContentType("text/plain;charset=utf-8");
		
		
		String username = request.getParameter("username");
		String username2 = request.getParameter("userName");
		String password = request.getParameter("password");
		
		PrintWriter out = response.getWriter();
		
		out.println("username = " + username + "\n");
		out.println("username2 = " + username2);
		out.println("password = " + password);
		
		/*getParameterValues*/
		String[] address = request.getParameterValues("address");
		
		if (address != null) {
			for (int index = 0; index < address.length; index ++) {
				out.println("address" + index + " = " + address[index]);
			}
		}
		
		/*getParameterMap**/
		Map map = request.getParameterMap();
		
		out.println("------------------" + "\n");
		out.println("Map:username = " + ((String[]) map.get("username"))[0] + "\n");
		out.println("Map:password = " + ((String[]) map.get("password"))[0]);
		
		String[] mapAddress = (String[]) map.get("address");
		
		if (mapAddress != null) {
			for (int index = 0; index < mapAddress.length; index ++) {
				out.println("mapAddress" + index + " = " + mapAddress[index]);
			}
		}
		
		out.println("------------------" + "\n");
		
		out.println("request method: " + request.getMethod());
		out.println("request URI: " + request.getRequestURI());
		out.println("request URL: " + request.getRequestURL());
		out.println("request protocol: " + request.getProtocol());
		out.println("request User-Agent: " + request.getHeader("User-Agent"));
		
	
		
		out.flush();
		out.close();
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req, resp);
	}

	@Override
	public void destroy() {
		super.destroy();
	}

	@Override
	public void init() throws ServletException {
		super.init();
		
	}

	@Override
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
	}

}
